const encoder = new TextEncoder()
const decoder = new TextDecoder()

export function abConcat () {
  let argsCount = arguments.length
  let arr = Array(argsCount)
  for (let i = 0; i < argsCount; i++) arr[i] = arguments[i]
  return arr.reduce(function (a, b) {
    if (a instanceof ArrayBuffer) a = new Uint8Array(a)
    if (b instanceof ArrayBuffer) b = new Uint8Array(b)
    let view = new Uint8Array(a.length + b.length)
    view.set(a, 0)
    view.set(b, a.length)
    return view
  })
}

export function ab2string (ab) {
  return decoder.decode(ab)
}

export function string2ab (str) {
  return encoder.encode(str)
}
